package com.zmn.plat.dubbo.impl.supplier;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.apache.curator.shaded.com.google.common.collect.Lists;
import org.apache.dubbo.config.annotation.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zmn.common.dto.VtDTO;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.manager.common.database.annotation.ReadOnlyConnection;
import com.zmn.plat.common.dto.supplier.ScmSupplierDRO;
import com.zmn.plat.dubbo.interfaces.supplier.ScmSupplierListRemoteService;
import com.zmn.plat.dubbo.utils.DubboConsts;
import com.zmn.plat.model.entity.supplier.ScmSupplierVO;
import com.zmn.plat.model.entity.supplier.SupplierCompanyMap;
import com.zmn.plat.services.interfaces.supplier.ScmSupplierCompanyMapService;
import com.zmn.plat.services.interfaces.supplier.ScmSupplierService;
import com.zmn.plat.utils.bean.BeanCopyUtils;

/**
 * 类描述： ScmSupplierRemoteServiceImpl
 *
 * @author DaiZhichao
 * @version v1.0
 * @since 2018/11/9 16:01
 */

@Service(loadbalance = "roundrobin", timeout = 5000, retries = -1, version = DubboConsts.INTERFACE_VERSION, validation = "true")
public class ScmSupplierListRemoteServiceImpl implements ScmSupplierListRemoteService {

    private Logger logger = LoggerFactory.getLogger(ScmSupplierListRemoteServiceImpl.class);

    @Resource
    ScmSupplierService scmSupplierService;
    @Resource
    ScmSupplierCompanyMapService supplierCompanyMapService;

    private static final String TAG = "供应商 dubbo 接口";

    /**
     * 描述:通过supplierId查询供应商
     *
     * @param supplierId
     *            Integer
     * @return ResponseDTO<PartCategoryDRO>
     * @author DaiZhichao
     * @since 2018/11/9 16:39
     */
    @Override
    @ReadOnlyConnection
    public ResponseDTO<ScmSupplierDRO> getBySupplierId(Integer supplierId) {

        if (Objects.isNull(supplierId)) {
            logger.error(String.format("[%s]参数:[%s]不能为空", TAG, "supplierId"));
            return ResponseDTO.fail("supplierId不能为空");
        }

        ScmSupplierVO supplierVO = scmSupplierService.findVOByKey(supplierId);
        List<SupplierCompanyMap> supplierCompanyMapList = supplierCompanyMapService.listBySupplierId(supplierId);
        if (Objects.isNull(supplierVO)) {
            logger.error(String.format("[%s]参数:[%s]对应的supplierVO对象不存在", TAG, "supplierId"));
            return ResponseDTO.fail(String.format("[%s]对应的supplierVO对象不存在", supplierId));
        }

        ScmSupplierDRO supplierDRO = new ScmSupplierDRO();

        try {
            BeanCopyUtils.copyProperties(supplierVO, supplierDRO);

            supplierDRO.setUseCompanyIdList(
                (!supplierCompanyMapList.isEmpty()) ? Lists.newArrayListWithExpectedSize(0) : supplierCompanyMapList.stream().map(SupplierCompanyMap::getCompanyId).collect(Collectors.toList()));

        } catch (Exception e) {
            logger.error(String.format("[%s]对象:[%s]拷贝出错,ex:[%s]", TAG, "CopyObjectUtil<supplier, supplierDRO>", e.getMessage()));
            return ResponseDTO.fail("拷贝ScmSupplier对象出错");
        }

        return ResponseDTO.success(supplierDRO, "OK");
    }

    /**
     * 描述:获取供应商简单列表
     *
     * @return ResponseDTO<PartCategoryDRO>
     * @author DaiZhichao
     * @since 2018/11/9 16:39
     */
    @Override
    @ReadOnlyConnection
    public ResponseDTO<List<VtDTO>> listSupplierSimpleByCompanyId(Integer companyId) {
        List<VtDTO> supplierSimpleList = supplierCompanyMapService.getSupplierSimpleList(companyId);
        if (supplierSimpleList.isEmpty()) {
            return ResponseDTO.fail("供应商列表为空");
        }
        return ResponseDTO.success(supplierSimpleList, "OK");
    }

}
